<template>
  <div>
    <vxe-toolbar id="toolbarId" :setting="{storage: true}" export :refresh="{query: findList}">
      <template v-slot:buttons>
        <el-button @click="findList">查询</el-button>
      </template>
    </vxe-toolbar>
    <vxe-table
      ref="xTable"
      border
      resizable
      stripe
      highlight-current-row
      highlight-hover-row
      show-overflow
      row-id="id"
      class="mytable-footer"
      height="700"
      :loading="loading"
      :footer-method="footerMethod"
      :footer-cell-class-name="footerCellClassName"
      :data="tableData"
    >
      <vxe-table-column>
        <vxe-table-column
          field="mainOrderNumber"
          title="货源单号"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.mainOrderNumber" placeholder="请输入货源单号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="orderCode"
          title="运输单号"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.orderCode" placeholder="请输入运输单号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="driverName"
          title="司机/车队名称"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.driverName" placeholder="请输入司机/车队名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="driverPhone"
          title="司机电话"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.driverPhone" placeholder="请输入司机电话" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="vehicleName"
          title="车牌号"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.vehicleName" placeholder="请输入车牌号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shipmentName"
          title="入驻商"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.shipmentName" placeholder="请输入入驻商名称" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="shipmentPhone"
          title="入驻商电话"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.shipmentPhone" placeholder="请输入入驻商电话" clearable />
        </template>
      </vxe-table-column>

      <vxe-table-column>
        <vxe-table-column
          field="contractNo"
          title="合同编号"
          show-header-overflow
          show-overflow
          min-width="200"
          sortable
        />
         <template v-slot:header="{ column }">
          <el-input v-model="listQuery.contractNo" placeholder="请输入合同编号" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="startAddress"
          title="出发地"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
         <template v-slot:header="{ column }">
          <el-input v-model="listQuery.startAddress" placeholder="请输入出发地" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="endAddress"
          title="目的地"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
         <template v-slot:header="{ column }">
          <el-input v-model="listQuery.endAddress" placeholder="请输入目的地" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="weight"
          title="净重"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.weight" placeholder="请输入净重" clearable />
        </template>
      </vxe-table-column>
      <!--<vxe-table-column>
        <vxe-table-column field="weight" title="净重" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.weightStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.weightEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>-->
      <!--<vxe-table-column>
        <vxe-table-column field="amount" title="金额" min-width="200" sortable />
        <template v-slot:header>
          <el-row>
            <el-col :span="11">
              <el-input v-model="listQuery.amountStart" placeholder="下限" clearable />
            </el-col>
            <el-col :span="11" :offset="2">
              <el-input v-model="listQuery.amountEnd" placeholder="上限" clearable />
            </el-col>
          </el-row>
        </template>
      </vxe-table-column>-->
      <vxe-table-column>
        <vxe-table-column
          field="amount"
          title="金额"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.amount" placeholder="请输入金额" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column
          field="goodsType"
          title="货物"
          show-header-overflow
          show-overflow
          min-width="160"
          sortable
        />
        <template v-slot:header="{ column }">
          <el-input v-model="listQuery.goodsType" placeholder="请输入货物" clearable />
        </template>
      </vxe-table-column>
      <vxe-table-column>
        <vxe-table-column field="gmtCreated" title="创建时间" min-width="420" sortable :resizable="false" />
        <template v-slot:header>
          <el-date-picker
            v-model="gmtCreatedRange"
            type="daterange"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptions"
            style="width:100%"
            range-separator="至"

            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="value => changeDate(value, 'gmtCreated')"
          />
        </template>
      </vxe-table-column>
      <vxe-table-column show-overflow title="操作" show-header-overflow width="100" fixed="right">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="详情" placement="top">
            <vxe-button type="text" @click="funcDetail(scope.row.contractPath)">
              <i class="el-icon-document" />
            </vxe-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="电子签章" placement="top">
            <vxe-button type="text" @click="funcSign(scope.row.code)">
              <i class="el-icon-s-check" />
            </vxe-button>
          </el-tooltip>
        </template>
      </vxe-table-column>
    </vxe-table>
    <!-- 详情 -->
    <vxe-modal v-model="detailFlag" title="详情" width="1200" height="600" resize>
      <contractDetail ref="detailRef" />
    </vxe-modal>
    <vxe-pager
      :loading="loading"
      :current-page.sync="listQuery.pageNum"
      :page-size.sync="listQuery.pageSize"
      :total="listQuery.total"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
      @page-change="handlePageChange"
    />
  </div>
</template>
<script>
import Vue from "vue";
import "xe-utils";
import VXETable from "vxe-table";
import "vxe-table/lib/index.css";
import { Script } from "vm";
import request from "@/utils/request";
import { fetchList } from "@/api/article";
import Pagination from "@/components/Pagination";
import contractDetail from "./components/contractDetail.vue";
import Sortable from "sortablejs";
import XEUtils from "xe-utils";
import VXEUtils from "vxe-utils";
Vue.use(VXETable, VXEUtils, XEUtils, {
  mounts: ["cookie"]
});
export default {
  data() {
    return {
      pickerOptions: {
        shortcuts: [
          {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }
        ]
      },
      gmtCreatedRange: '',
      message: "",
      loading: false,
      tableData: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      value2: "",
      detailFlag: false,
      listQuery: {
        contractType: 0, //合同类型，0承运，1托运，默认0
        mainOrderNumber: "",
        orderCode: "",
        driverName: "",
        driverPhone: "",
        vehicleName: "",
        shipmentName: "",
        shipmentPhone: "",
        contractNo: "",
        startAddress: "",
        endAddress: "",
        weight: "",
        amount: "",
        goodsType: "",
        gmtCreated: "",
        gmtCreatedStart: "",
        gmtCreatedEnd: "",
        balanceStart: "",
        balanceEnd: "",
        weightStart: "",
        weightEnd: "",
        pageNum: 1,
        pageSize: 20,
        total: 0,
        sortField: "",
        sortType: ""
      }
    };
  },
  components: {
    contractDetail
  },
  created() {
    this.findList();
  },
  methods: {
    findList() {
      this.loading = true;
      return new Promise(resolve => {
        request({
          url: "/arpa-basic-api/orderDetailContract",
          method: "get",
          params: this.listQuery
        }).then(response => {
          this.tableData = response.data.records;
          this.listQuery.total = response.data.total;
          this.loading = false;
          resolve();
        });
      });
    },
    // 时间
    changeDate(value, prefix) {
      if (value) {
        this.listQuery[`${prefix}Start`] = value[0] + " 00:00:00";
        this.listQuery[`${prefix}End`] = value[1] + " 23:59:59";
      } else {
        this.listQuery[`${prefix}Start`] = "";
        this.listQuery[`${prefix}End`] = "";
      }
    },
    // 获取详情
    funcDetail(path) {
      window.open(path)
    },
    //电子签章
    funcSign(code) {
      this.$confirm("确认进行电子签章？").then(_ => {
        request({
          url: `/ntocc-tps-api/elecSign/${code}`,
          method: "get"
        }).then(response => {
          this.$message({ message: response.msg, type: "success" });
          this.findList();
        });
      });
    },
    // 创建时间
    dataPicker(value) {
      if (value) {
        this.listQuery.gmtCreatedStart = value[0];
        this.listQuery.gmtCreatedEnd = value[1];
      } else {
        this.listQuery.gmtCreatedStart = "";
        this.listQuery.gmtCreatedEnd = "";
      }
    },
    handlePageChange({ currentPage, pageSize }) {
      this.listQuery.page = currentPage;
      this.listQuery.limit = pageSize;
      this.findList();
    },
    footerCellClassName({ $rowIndex, column, columnIndex }) {
      if (columnIndex === 0) {
        if ($rowIndex === 0) {
          return "col-blue";
        } else {
          return "col-red";
        }
      }
    },
    footerMethod({ columns, data }) {
      return [
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "平均";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.mean(data, column.property);
          }
          return null;
        }),
        columns.map((column, columnIndex) => {
          if (columnIndex === 0) {
            return "和值";
          }
          if (["pageviews", "forecast"].includes(column.property)) {
            return XEUtils.sum(data, column.property);
          }
          return null;
        })
      ];
    }
  }
};
</script>

<style>
.vxe-table .vxe-body--column:not(.col--ellipsis),
.vxe-table .vxe-footer--column:not(.col--ellipsis),
.vxe-table .vxe-header--column:not(.col--ellipsis) {
  padding: 6px 0;
}

.vxe-toolbar {
  padding: 0 0 10px 0;
}
</style>
